﻿using Hotel.Application.DicTypes;
using Hotel.Application;
using Hotel.Domain;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using Hotel.Application.Dics.Dto;
using Hotel.Application.Dics;
using Hotel.Web.Filters;

namespace Hotel.Web.Controllers
{
    public class DicController : BaseController<Dic, DicCreateUpdateDto, DicListDto>
    {
        private readonly IDicService dicService;

        public DicController(IBaseService<Dic, DicCreateUpdateDto, DicListDto> service, IDicService dicService) : base(service)
        {
            this.dicService = dicService;
        }

        [HttpGet]
        public async Task<IActionResult> ToPageListAsync(DicQueryDto dto, int PageSize = 10, int PageIndex = 1)
        {
            Expressionable<Dic> exp = new Expressionable<Dic>();
            exp.AndIF(dto.TypeId != null, it => it.TypeId == dto.TypeId);
            exp.AndIF(!string.IsNullOrWhiteSpace(dto.Keywords), it => it.DicName.Contains(dto.Keywords));
            exp.AndIF(dto.StartTime != null, it => it.CreateTime >= dto.StartTime);
            exp.AndIF(dto.EndTime != null, it => it.CreateTime <= dto.EndTime);
            var page = await base.PageAsync(exp, PageSize, PageIndex);
            return page;
        }

        [HttpPost]
        public async Task<IActionResult> CheckName(DicCreateUpdateDto dto)
        {
            return Ok(await dicService.CheckName(dto));
        }

        [HttpGet]
        public async Task<IActionResult> QueryByCache(string TypeCode)
        {
            return new JsonResult(await dicService.QueryByCacheAsync(TypeCode));
        }
    }
}
